/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ZakEdit.java
 *
 * Created on 12.6.2011, 13:59:02
 */
package forms;

import java.util.Iterator;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;
import java.lang.StringBuilder;
import src.Evidence;
import src.Krouzek;
import src.SouborIO;
import src.Zak;

/**
 *
 * @author Viktor
 */
public class ZakEdit extends javax.swing.JFrame {
Evidence e;
ViewPanel v;
Object[] listitems;
SouborIO io;
boolean create;
Zak vybr;
    /** Creates new form ZakEdit */
    public ZakEdit(Evidence e, ViewPanel v, boolean create, Zak vybr) {
        io = new SouborIO();
        this.e = e;
        this.v = v;
        initComponents();
        setSize(600, 400);
        listitems = new Object[30];
        zobrazKrouzky();
        this.create = create;
        this.vybr = vybr;
        rozhodni();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jmn = new javax.swing.JTextField();
        prijm = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tlf = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        dnDEN = new javax.swing.JTextField();
        dnMesic = new javax.swing.JTextField();
        dnRok = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        adr = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        KrouzkyList = new javax.swing.JList();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Jméno :");

        jLabel2.setText("Příjmení :");

        jmn.setText("Jaroslav");

        prijm.setText("Novák");

        jLabel3.setText("Telefon :");

        tlf.setText("777777777");

        jLabel4.setText("Datum narození :");

        dnDEN.setText("1");
        dnDEN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dnDENActionPerformed(evt);
            }
        });

        dnMesic.setText("1");

        dnRok.setText("2000");

        jLabel5.setFont(new java.awt.Font("Tahoma", 2, 10));
        jLabel5.setText("( den, měsíc, rok)");

        jLabel6.setText("Adresa :");

        adr.setColumns(20);
        adr.setRows(5);
        jScrollPane1.setViewportView(adr);

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Zrušit");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        KrouzkyList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        KrouzkyList.setToolTipText("Více kroužků vyberete pomoci Ctrl + kliknutí");
        jScrollPane2.setViewportView(KrouzkyList);

        jLabel7.setText("Seznam dostupných kroužků :");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel4)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(tlf)
                                .addComponent(prijm)
                                .addComponent(jmn, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(dnDEN, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(dnMesic, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(dnRok, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 80, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(123, 123, 123)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jmn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(prijm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(tlf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(dnDEN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(dnMesic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(dnRok, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 111, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton2))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void dnDENActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dnDENActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_dnDENActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        String datnar = dnDEN.getText()+"."+dnMesic.getText()+"."+dnRok.getText();
        Zak z = new Zak(prijm.getText(),jmn.getText(), tlf.getText(), datnar, adr.getText());
        pridatKrouzky(z);
        e.pridejDoEvidence(z);
        v.update();
        v.repaint();
        io.ulozit("evidence.bin", e);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList KrouzkyList;
    private javax.swing.JTextArea adr;
    private javax.swing.JTextField dnDEN;
    private javax.swing.JTextField dnMesic;
    private javax.swing.JTextField dnRok;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jmn;
    private javax.swing.JTextField prijm;
    private javax.swing.JTextField tlf;
    // End of variables declaration//GEN-END:variables

    private void zobrazKrouzky() {
        listitems[0]= "<žádný>";
        int pocet = 1;
        for (Iterator it = e.getSeznamKrouzku().iterator(); it.hasNext();) {
            Krouzek kr = (Krouzek) it.next();
            listitems[pocet] = kr.getNazev();
            pocet++;
        }
        
        KrouzkyList.setListData(listitems);
    }

    private void pridatKrouzky(Zak z) {
        
   int[] pom =  KrouzkyList.getSelectedIndices();
        
        for (int i = 0; i < pom.length; i++) {
            if(pom[i]== 0){
               break;
            }
             z.priradKrouzek(e.getSeznamKrouzku().get(pom[i]-1)); 
        }
   
    }

    private void rozhodni() {
        if(!create){
            if(vybr != null){
              prijm.setText(vybr.getPrijmeni());              
              jmn.setText(vybr.getJmeno());
              tlf.setText(vybr.getTelefon());                  
              adr.setText(vybr.getBydliste());
       //       StringBuilder sb = new StringBuilder(vybr.getDatNar());
//             char[] ch1 = new char[2];
//             char[] ch2 = new char[2];
//             char[] ch3 = new char[4];
//             
//             vybr.getBydliste().getChars(0, 2, ch1, 0);
//               vybr.getBydliste().getChars(3, 5, ch2, 0);
//                 vybr.getBydliste().getChars(6, 10, ch3, 0);
//              dnDEN.setText(String.copyValueOf(ch1));    
//                dnMesic.setText(String.copyValueOf(ch2)); 
//                dnRok.setText(String.copyValueOf(ch3));
                
                
                
                
                
                
            }
            
            
            
            
        }
        
        
        
    }
}
//2412 radku kodu ..pekne, ne ?